package com.zsf.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zsf.pojo.Administrators;
import com.zsf.pojo.Product;
import com.zsf.service.ProductService;
import com.zsf.service.TagService;
import com.zsf.serviceImp.ProductServiceImp;
import com.zsf.serviceImp.TagServiceImp;

/**
 * Servlet implementation class ProductServlet
 */
@WebServlet("/product")
public class ProductServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	ProductService productService=new ProductServiceImp();
	TagService tagService=new TagServiceImp();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ProductServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {	
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		Administrators administrators=(Administrators) request.getSession().getAttribute("current_admin");
		PrintWriter pw=response.getWriter();
		if(administrators==null){
			pw.print("<script>alert('请先登录！'); window.location.href='login.jsp';</script>");
		}else{
			List<Product> products=productService.getProductAll();
			List tags=tagService.getTagAll();
			request.getSession().setAttribute("tags", tags);
			request.setAttribute("products", products);
			request.getRequestDispatcher("product.jsp").forward(request, response);
		}
	}

}
